package com.wcpdoc.exam.api.controller;

import com.wcpdoc.base.entity.User;
import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.util.StringUtil;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.service.UserQuestionTypeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 某个题库有权限的人管理
 */
@RestController
@RequestMapping("api/userQuestion")
public class ApiUserQuestionController extends BaseController {

    @Resource
    private UserQuestionTypeService userQuestionTypeService;

    /**
     * 或某个题库的有权限的人
     * @return
     */
    @RequestMapping("/listpage")
    @ResponseBody
    public PageResult listpage() {
        try {
            PageIn pageIn = new PageIn(request);
            if (!ValidateUtil.isValid(pageIn.get("questionTypeId"))) {
                return PageResult.err().msg("请选择题库");
            }
            PageOut pageOut = userQuestionTypeService.getListpage(pageIn);
            return PageResultEx.ok().data(pageOut);
        } catch (Exception e) {
            return PageResult.err();
        }
    }

    /**
     * 或某个题库的有权限的人
     * @param questionTypeId
     * @return
     */
    @RequestMapping("/getUserListByQuestionTypeId")
    @ResponseBody
    public PageResult getUserListByQuestionTypeId(Integer questionTypeId) {
        if(questionTypeId == null){
            return PageResult.err().msg("请选择题库！");
        }
        return userQuestionTypeService.getUserListByQuestionTypeId(questionTypeId);

    }

    /**
     * 设置某个题库的有权限的人
     * @return
     */
    @RequestMapping("/setting")
    @ResponseBody
    public PageResult setting(Integer questionTypeId,@RequestParam List<Integer> userIdList) {
        if(questionTypeId == null){
            return PageResult.err().msg("请选择题库！");
        }
        if(userIdList == null||userIdList.isEmpty()){
            return PageResult.err().msg("请选择用户！");
        }
        return userQuestionTypeService.setting(questionTypeId,userIdList, getCurUser().getId());
    }
}
